package com.springBoot.springBootSysCore.modules.controller.web.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.springBoot.springBootSysCore.common.service.jpaQuery.IBaseController;
import com.springBoot.springBootSysCore.modules.controller.web.base.BaseController;
import com.springBoot.springBootSysCore.modules.entity.system.SysCode;
import com.springBoot.springBootSysCore.modules.entity.system.SysDict;
import com.springBoot.springBootSysCore.modules.entity.system.SysRole;
import com.springBoot.springBootSysCore.modules.enums.base.DeleteStateEnum;
import com.springBoot.springBootSysCore.modules.repository.system.SysResourceRepository;
import com.springBoot.springBootSysCore.modules.repository.system.SysRoleRepository;
import com.springBoot.springBootSysCore.modules.repository.system.SysUserRepository;
import com.springBoot.springBootSysCore.modules.services.system.SysResourceService;
import com.springBoot.springBootSysCore.modules.services.system.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理员平台--角色Controller
 * @author PSC
 * 2015年11月27日
 */
@Controller
@RequestMapping("${adminPath}/role")
public class SysRoleController extends BaseController<SysRole, String> {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysResourceService sysResourceService;


	@ModelAttribute("entity")
	public SysRole get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return sysRoleService.findById(id);
		} else {
			return new SysRole();
		}
	}

	/**
	 * 加载角色管理页面信息
	 * @param pageNumber
	 * @param pageSize
	 * @param model
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("queryByPage")
	@ResponseBody
	public Object queryRoleByPageForJson(HttpServletRequest request,
    		@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
			Model model) throws JsonProcessingException {
//		super.queryByPage(pageNumber, pageSize, model, request, "createDate", "desc");
//        return "system/role/roleList";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("EQ_delFlag", DeleteStateEnum.NO_DELETE.getVal());
		return super.queryByPageForLayuiTables(model, request,map, "createDate", "desc");
	}
	
	/**
	 * 到修改或新增页面
	 * @author PSC
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("findOne")
	public String findOne(@RequestParam(required=false)String id, Model model,String detail){
		model.addAttribute("detail",detail);
		return "system/role/roleChange";
	}
	
	/**
	 * 保存角色实现
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("saveEntity")
	@ResponseBody
	public  Object saveEntity(SysRole sysRole,String resIds) throws JsonProcessingException {
		return sysRoleService.saveEntity(sysRole,resIds);
	}
	
//	/**
//	 * 删除角色
//	 * @param roleIds
//	 * @return
//	 * @throws JsonProcessingException
//	 */
//	@RequestMapping("deleteEntity")
//	@ResponseBody
//	public Object deleteEntity(@RequestParam(value="roleIds[]",required=true)String[] roleIds) throws JsonProcessingException {
//		return sysRoleService.deleteEntity(Arrays.asList(roleIds));
//	}

	@RequestMapping(value="deleteEntity",method= RequestMethod.POST)
	@ResponseBody
	public Object deleteEntity(SysRole sysRole) throws JsonProcessingException{
		return sysRoleService.deleteEntity(sysRole);
	}

	/**
	 * 获取角色的资源zTree信息
	 * @author PSC
	 * @param roleId
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("getResourceListDateForRole")
	@ResponseBody
	public Object getResourceListDateForRole(String roleId) throws JsonProcessingException {
		return writeValueAsString(sysResourceService.findTreeRes(roleId));
	}

	/**
	 * 查询所有公共的权限角色
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("findAllPublic")
	@ResponseBody
	public Object findAllPublic() throws JsonProcessingException {
		return writeValueAsString(sysRoleService.findAllPublic());
	}

}
